//
//  CreateContractViewController.m
//  ERP
//
//  Created by goldeney on 15/5/29.
//  Copyright (c) 2015年 ShangHai Goldeneye Techonology Co.,Ltd. All rights reserved.
//

#import "MTCCreateContractViewController.h"
#import "GEInputTableViewCell.h"
#import "MTCSelectClienterViewController.h"
#import "MTCConcreteConfirmationViewController.h"

#import "BGRadioView.h"
#import "JGActionSheet.h"

#import "NSDictionary+DataFormat.h"
#import "NSMutableDictionary+Format.h"
#import "GEPickerTextField.h"
#import "NSMutableArray+Format.h"

@interface MTCCreateContractViewController ()

@property (nonatomic, strong) NSMutableDictionary *orderInfo;

@end

@implementation MTCCreateContractViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:@selector(submitContractSelector:)];
        self.navigationItem.rightBarButtonItem = leftButton;
    }
    return self;
}

- (void)getPassingParameter:(id)object
{
    if (!object) {
        [self setTitle: @"新建合同"];
    } else {
        [self setTitle: @"修改合同"];
        NSDictionary *dic = object;
        self.orderInfo = [NSMutableDictionary dictionaryWithDictionary:dic];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 10)];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 10)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)submitContractSelector:(id)selector
{
    GEInputTableViewCell *geViewCell = (GEInputTableViewCell*)[self.tableView getCellViewInRow:0 inSection:0];
    GEPickerTextField *textField = [self.tableView getCellSaveClass:[UITextField class] pathForRow:2 inSection:0];
    
    UITextField *textField01 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:0];
    UITextField *textField03 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:3 inSection:0];
    UITextField *textField04 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:5 inSection:0];
    UITextField *textField07 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:7 inSection:0];
    UITextField *textField09 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:9 inSection:0];
    
    if (!geViewCell.objectData) {
        [self showAlertView:@"请选择客户名称"];
    } else if (textField01.text.length<=0) {
        [self showAlertView:@"请输入合同名称"];
    } else if (!textField.selectData) {
        [self showAlertView:@"请选择合同性质"];
    } else if (textField07.text.length<=0) {
        [self showAlertView:@"请输入施工单位"];
    } else if (textField03.text.length<=0) {
        [self showAlertView:@"请输入签订方量"];
    } else if ([textField03.text floatValue] <0) {
        [self showAlertView:@"签订方量必须大于0方"];
    } else if (textField04.text.length<=0) {
        [self showAlertView:@"请选择签订日期"];
    } else if (textField09.text.length<=0) {
        [self showAlertView:@"请选择分发站点"];
    } else {
        //数据已验证，进行数据封包
        [self newContractDataSealPackage];
    }
}

//新建合同打包
- (void)newContractDataSealPackage
{
    GEInputTableViewCell *geViewCell = (GEInputTableViewCell*)[self.tableView getCellViewInRow:0 inSection:0];
    UITextField *textField01 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:0];
    GEPickerTextField *textField = [self.tableView getCellSaveClass:[UITextField class] pathForRow:2 inSection:0];
    
    UITextField *textField03 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:3 inSection:0];
    UITextField *textField05 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:5 inSection:0];
    UITextField *textField06 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:6 inSection:0];
    UITextField *textField14 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:7 inSection:0];
    UITextField *textField15 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:8 inSection:0];
    
    UITextField *textField07 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:0 inSection:1];
    UITextField *textField08 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:1];
    UITextField *textField09 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:2 inSection:1];
    UITextField *textField10 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:3 inSection:1];
    
    UITextField *textField11 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:0 inSection:2];
    UITextField *textField12 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:2];
    UITextField *textField13 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:2 inSection:2];
    
    UISwitch *mSwitch = [self.tableView getCellSaveClass:[UISwitch class] pathForRow:4 inSection:0];
    NSString *HTBH = [self.orderInfo objectFormatStringForKey:@"合同编号"];
    
    NSMutableDictionary *dataDictionary = [NSMutableDictionary dictionary];
    [dataDictionary setFormatObject:HTBH forKey:@"合同编号"];
    [dataDictionary setFormatObject:geViewCell.objectData forKey:@"客户名称"];
    [dataDictionary setFormatObject:textField01.text forKey:@"合同名称"];
    [dataDictionary setFormatObject:textField.selectData forKey:@"合同性质"];
    [dataDictionary setFormatObject:textField03.text forKey:@"签订方量"];
    [dataDictionary setFormatObject:[NSNumber numberWithBool:mSwitch.on] forKey:@"方量控制"];
    [dataDictionary setFormatObject:textField05.text forKey:@"签订日期"];
    [dataDictionary setFormatObject:textField06.text forKey:@"建设单位"];
    [dataDictionary setFormatObject:textField14.text forKey:@"施工单位"];
    [dataDictionary setFormatObject:textField15.text forKey:@"第三方单位"];
    
    [dataDictionary setFormatObject:textField07.text forKey:@"工地地址" defaults:[self.orderInfo objectFormatStringForKey:@"工地地址"]];
    [dataDictionary setFormatObject:textField08.text forKey:@"运输距离" defaults:[self.orderInfo objectFormatStringForKey:@"运输距离"]];
    [dataDictionary setFormatObject:textField09.text forKey:@"工地联系人" defaults:[self.orderInfo objectFormatStringForKey:@"工地联系人"]];
    [dataDictionary setFormatObject:textField10.text forKey:@"工地联系电话" defaults:[self.orderInfo objectFormatStringForKey:@"工地联系电话"]];
    
    [dataDictionary setFormatObject:textField11.text forKey:@"业务人员" defaults:[self.orderInfo objectFormatStringForKey:@"业务人员"]];
    [dataDictionary setFormatObject:textField12.text forKey:@"业务员电话" defaults:[self.orderInfo objectFormatStringForKey:@"业务员电话"]];
    [dataDictionary setFormatObject:textField13.text forKey:@"备注" defaults:[self.orderInfo objectFormatStringForKey:@"备注"]];
    
    UITextField *textField19 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:9 inSection:0];
    GEInputTableViewCell *geViewCell09 = (GEInputTableViewCell*)[self.tableView getCellViewInRow:9 inSection:0];
    [dataDictionary setFormatObject:geViewCell09.objectData forKey:@"分发站点编号"];
    [dataDictionary setFormatObject:textField19.text forKey:@"分发站点名称"];
    
    MTCConcreteConfirmationViewController *view = [[MTCConcreteConfirmationViewController alloc] init];
    [view setValue:dataDictionary];
    [self.navigationController pushViewController:view animated:YES];
}


#pragma mark - UIAlertView

- (void)showAlertView:(NSString*)message
{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"温馨提示" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    alert.alertViewStyle=UIAlertViewStyleDefault;
    [alert show];
}


#pragma mark
#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0) return 10;//通用方法
    else if(section == 1) return 4;
    else if(section == 2) return 3;
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"CreateContractViewController%ld%ld",indexPath.section,indexPath.row];
    
    //合同基本信息
    if(indexPath.section == 0){
        GEInputTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell != nil) return cell;
        
        if(indexPath.row == 0){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.titleName = @"客户名称";
            cell.placeholder = @"请选择客户名称";
            if (self.orderInfo)
            cell.isMark = YES;
            cell.isEdit = NO;
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            if(self.orderInfo) {
                NSMutableDictionary *clienterDic = [NSMutableDictionary dictionary];
                [clienterDic setObject:[self.orderInfo objectFormatIntegerNumberForKey:@"客户ID"] forKey:@"客户ID"];
                [clienterDic setObject:[self.orderInfo objectFormatIntegerNumberForKey:@"客户编号"] forKey:@"客户编号"];
                [clienterDic setObject:[self.orderInfo objectFormatStringForKey:@"客户名称"] forKey:@"客户名称"];
                cell.objectData = clienterDic;
                cell.inputFiled = [self.orderInfo objectFormatStringForKey:@"客户名称"];
            }
            
        } else if(indexPath.row == 1){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"合同名称";
            cell.placeholder = @"请输入合同名称";
            cell.isMark = YES;
            cell.isEdit = YES;
            if(self.orderInfo) cell.inputFiled = [self.orderInfo objectFormatStringForKey:@"合同名称"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 2){
            WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
            [content setInterfaceType:InterfaceTypeGroup];
            [content setModuleType:@"HTGL_JCXX_LIST"];
            [content setResultBackType:ResultBackTypeCommonUnordered];
            [content addParameterKey:@"Type" value:@1];
            
            cell = [[GEInputTableViewCell alloc] initWithSelectReuseIdentifier:CellIdentifier request:content];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.titleName = @"合同性质";
            cell.placeholder = @"请选择合同性质";
            cell.isMark = YES;
            //[cell addTarget:self action:@selector(textFieldDidChange:)];
            [cell setEnablePrevious:NO next:NO];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
            if (self.orderInfo) {
                GEPickerTextField *textField = [self.tableView getCellSaveClass:[UITextField class] pathForRow:2 inSection:0];
                NSMutableDictionary *clienterDic = [NSMutableDictionary dictionary];
                NSInteger HTXZ = [self.orderInfo objectFormatIntegerForKey:@"Status"];
                if(HTXZ >0){ //合同性质必须大于0，默认1开始
                    [clienterDic setObject:[self.orderInfo objectFormatIntegerNumberForKey:@"Status"] forKey:@"Status"];
                    [clienterDic setObject:[self.orderInfo objectFormatStringForKey:@"合同性质"] forKey:@"Definition"];
                    textField.selectData = clienterDic;
                    cell.inputFiled = [self.orderInfo objectFormatStringForKey:@"合同性质"];
                }
            }
            
        } else if(indexPath.row == 3){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"签订方量";
            cell.placeholder = @"请输入签订方量(立方)";
            cell.isMark = YES;
            cell.isEdit = YES;
            if(self.orderInfo) cell.inputFiled = [self.orderInfo objectFormatStringForKey:@"签订方量"];
            [cell setKeyboardType:UIKeyboardTypeDecimalPad filter:@"0123456789" block:
             ^BOOL(UITextField *textField, NSRange range, NSString *string) {
                 //第二个不能是零,且第一个是零后自动清除
                 if (textField.text.length == 1 && [textField.text hasPrefix:@"0"]){
                     textField.text = string;
                     return NO;
                 }
                 return YES;
             }];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 4){
            cell = [[GEInputTableViewCell alloc] initWithSwitchIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"方量控制";
            cell.isMark = NO;
            if(self.orderInfo) {
                NSInteger FLKZ = [self.orderInfo objectFormatIntegerForKey:@"FLKZ"];
                UISwitch *mSwitch = [self.tableView getCellSaveClass:[UISwitch class] pathForRow:indexPath.row inSection:indexPath.section];
                mSwitch.on = FLKZ == 2 ? YES : NO;
            }
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 5){
            cell = [[GEInputTableViewCell alloc] initWithDateReuseIdentifier:CellIdentifier];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.titleName = @"签订日期";
            cell.placeholder = @"请选择签订日期";
            cell.isMark = YES;
            [cell setEnablePrevious:NO next:NO];
            [cell setDatePickerMode:UIDatePickerModeDate];
            if(self.orderInfo) cell.inputFiled = [self.orderInfo objectFormatStringForKey:@"签订日期"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 6){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"建设单位";
            cell.placeholder = @"请输入建设单位";
            cell.isMark = NO;
            cell.isEdit = YES;
            if(self.orderInfo) cell.inputFiled = [self.orderInfo objectFormatStringForKey:@"建设单位"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 7){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"施工单位";
            cell.placeholder = @"请输入施工单位";
            cell.isMark = YES;
            cell.isEdit = YES;
            if(self.orderInfo) cell.inputFiled = [self.orderInfo objectFormatStringForKey:@"施工单位"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 8){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"第三方单位";
            cell.placeholder = @"请输入第三方单位";
            cell.isMark = NO;
            cell.isEdit = YES;
            if(self.orderInfo) cell.inputFiled = [self.orderInfo objectFormatStringForKey:@"第三方单位"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 9){
            cell = [[GEInputTableViewCell alloc] initWithSelectReuseIdentifier:CellIdentifier request:nil];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.titleName = @"分发站点";
            cell.placeholder = @"请选择分发站点";
            cell.isMark = YES;
            [cell setEnablePrevious:NO next:NO];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
        }
        
        return cell;
    }
    
    //工地信息
    else if(indexPath.section == 1){
        GEInputTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell != nil) return cell;
        
        if(indexPath.row == 0){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"工地地址";
            cell.placeholder = @"请输入施工单位";
            cell.isMark = NO;
            cell.isEdit = YES;
            if(self.orderInfo) cell.inputFiled = [self.orderInfo objectFormatStringForKey:@"工地地址"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 1){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"运输距离";
            cell.placeholder = @"请输入运输距离(公里)";
            cell.isMark = NO;
            cell.isEdit = YES;
            [cell setKeyboardType:UIKeyboardTypeDecimalPad filter:@"0123456789."];
            if(self.orderInfo) cell.inputFiled = [self.orderInfo objectFormatStringForKey:@"运输距离"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 2){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"工地联系人";
            cell.placeholder = @"请输入工地联系人";
            cell.isMark = NO;
            cell.isEdit = YES;
            if(self.orderInfo) cell.inputFiled = [self.orderInfo objectFormatStringForKey:@"工地联系人"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 3){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"工地联系电话";
            cell.placeholder = @"请输入联系电话";
            cell.isMark = NO;
            cell.isEdit = YES;
            [cell setKeyboardType:UIKeyboardTypeDecimalPad filter:@"0123456789+"];
            if(self.orderInfo) cell.inputFiled = [self.orderInfo objectFormatStringForKey:@"工地联系电话"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        }
        return cell;
    }
    
    //其他信息
    else if(indexPath.section == 2){
        GEInputTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell != nil) return cell;
        else if(indexPath.row == 0){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"业务人员";
            cell.placeholder = @"请输入业务人员";
            cell.isMark = NO;
            cell.isEdit = YES;
            if(self.orderInfo) cell.inputFiled = [self.orderInfo objectFormatStringForKey:@"业务人员"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 1){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"业务员电话";
            cell.placeholder = @"请输入业务人员电话";
            cell.isMark = NO;
            cell.isEdit = YES;
            [cell setKeyboardType:UIKeyboardTypeDecimalPad filter:@"0123456789+"];
            if(self.orderInfo) cell.inputFiled = [self.orderInfo objectFormatStringForKey:@"业务员电话"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 2){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"备        注";
            cell.placeholder = @"可不填";
            cell.isMark = NO;
            cell.isEdit = YES;
            if(self.orderInfo) cell.inputFiled = [self.orderInfo objectFormatStringForKey:@"备注"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
        }
        
        return cell;
    }
    
    return nil;
}

#pragma mark
#pragma mark - UITableViewDelegate

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if(indexPath.section == 0 && indexPath.row == 0){ //客户名称选择
        MTCSelectClienterViewController *view = [[MTCSelectClienterViewController alloc] init];
        AbstractNavigationController *navigationController = [[AbstractNavigationController alloc] initWithRootViewController:view];
        view.backResultDelagate = self;
        [self presentViewController:navigationController animated:YES completion:nil];
        
    } else if(indexPath.section == 0 && indexPath.row == 2){ //合同性质
        GEInputTableViewCell *geViewCell = (GEInputTableViewCell*)[self.tableView getCellViewInRow:2 inSection:0];
        NSArray *resultArray = geViewCell.objectData;
        if (resultArray) {
            NSDictionary *QDDJ = [NSDictionary dictionaryWithObject:@"%@" forKey:@"Definition"];
            [geViewCell setPickerTextFieldComponents:resultArray keyValue:@[QDDJ]];
        }
        
    } else if(indexPath.section == 0 && indexPath.row == 4){ //方量控制
        UISwitch *mSwitch = [self.tableView getCellSaveClass:[UISwitch class] pathForRow:indexPath.row inSection:indexPath.section];
        [mSwitch setOn:!mSwitch.on animated:YES];
        
    } else if(indexPath.section == 0 && indexPath.row == 9){ //分发站点
        [self distributeStionNetWorkContractData];
        return;
    }
    
    //键盘事件处理
    UITextField *textField = [self.tableView getCellSaveClass:[UITextField class] pathForRow:indexPath.row inSection:indexPath.section];
    if (textField) [textField becomeFirstResponder];
    
}

#pragma mark -
#pragma mark 网络获取分发站点

- (void)distributeStionNetWorkContractData
{
    JGProgressHUD *HUD = [self showSimpleHUDText:@"获取站点中"];
    
    WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
    [content setInterfaceType:InterfaceTypeGroup];
    [content setModuleType:@"HTGL_JCXX_LIST"];
    [content setResultBackType:ResultBackTypeCommonUnordered];
    [content addParameterKey:@"Type" value:@3];
    
    [content startAsyncConnectCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        [HUD dismissAnimated:YES];//关闭进度框
        
        //添加数据，刷新页面
        NSArray *firstArray = nil;
        if(result.resultArray && result.resultArray.count>0)
            firstArray = [result.resultArray firstObject];
        
        //数据处理
        if(firstArray && firstArray.count>0){
            [self distributeActionSheetStionHandle:firstArray];
        } else {
            [self showErrorHUDText:@"无分发站点"];
        }
        
    } failed:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        [HUD dismissAnimated:YES];//关闭进度框
        [self showErrorHUDText:@"获取失败"];
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        [HUD dismissAnimated:YES];//关闭进度框
        [self showErrorHUDText:@"网络异常"];
        
    }];
}

//分发合同弹出框
- (void)distributeActionSheetStionHandle:(NSArray*)stationArray
{
    NSMutableArray *sortByItemsArray = [[NSMutableArray alloc] init];
    for(int i=0;i<stationArray.count;i++){
        NSDictionary *mDictionary = [stationArray objectAtIndex:i];
        [sortByItemsArray addObject:[mDictionary objectFormatStringForKey:@"U_Name"]];
    }
    
    CGFloat height = sortByItemsArray.count * 44;
    BGRadioView *radioViewSortBy = [[BGRadioView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth - 40, height)];
    radioViewSortBy.rowItems =  sortByItemsArray;
    radioViewSortBy.maxRow = [sortByItemsArray count];
    radioViewSortBy.isMultiSelect = YES;
    
    JGActionSheetSection *sheetSection = [JGActionSheetSection sectionWithTitle:@"分发站点选择" message:@"请选择分发的站点(可多选)：" buttonTitles:@[@"确定"] buttonStyle:JGActionSheetButtonStyleDefault contentView:radioViewSortBy];
    
    JGActionSheetSection *sheetSectionCancel = [JGActionSheetSection sectionWithTitle:nil message:nil buttonTitles:@[@"取消"] buttonStyle:JGActionSheetButtonStyleCancel];
    
    JGActionSheet *simple = [JGActionSheet actionSheetWithSections:@[sheetSection, sheetSectionCancel]];
    [simple setButtonPressedBlock:^(JGActionSheet *sheet, NSIndexPath *indexPath) {
        //合同分发
        if (indexPath.section == 0 && indexPath.row == 0) {
            NSInteger dataCount = radioViewSortBy.selectArray.count;
            if (dataCount <= 0) return; //未选择任何数据
            //多个数据汇合整理
            NSMutableString *StationIDs = [NSMutableString string];
            NSMutableString *StationName = [NSMutableString string];
            for (int i=0; i<dataCount;i++) {
                NSNumber *select = [radioViewSortBy.selectArray objectAtIndex:i];
                NSDictionary *mDictionary = [stationArray objectAtIndex:[select integerValue]];
                
                NSString *StationID = [mDictionary objectFormatStringForKey:@"Code"];
                [StationIDs appendFormat:@"%@%@", StationID, (i < dataCount - 1 ? @",": @"")];
                
                NSString *stationNameValue = [mDictionary objectFormatStringForKey:@"U_Name"];
                [StationName appendFormat:@"%@%@", stationNameValue, (i < dataCount - 1 ? @",": @"")];
                
            }
            GEInputTableViewCell *geViewCell = (GEInputTableViewCell*)[self.tableView getCellViewInRow:9 inSection:0];
            geViewCell.objectData = StationIDs;
            
            UITextField *textField = [self.tableView getCellSaveClass:[UITextField class] pathForRow:9 inSection:0];
            textField.text = StationName;
        }
        [sheet dismissAnimated:YES];
    }];
    [simple showInView:self.navigationController.view animated:YES];
    
    //多选择回执
    [radioViewSortBy setSelectMultiBlock:^(NSMutableArray *array) {
        if(array && array.count > 0)
            [sheetSection setButtonStyle:JGActionSheetButtonStyleRed forButtonAtIndex:0];
        else [sheetSection setButtonStyle:JGActionSheetButtonStyleDefault forButtonAtIndex:0];
    }];
}

/************************************************************************/

#pragma mark
#pragma mark - BackResultDataDelagate

- (void) backResultDataKey:(id)key dataValue:(id)backValue
{
    if (backValue) {
        //客户名称选择
        NSDictionary *clienterDictionary = backValue;
        GEInputTableViewCell *clienterCell = (GEInputTableViewCell*)[self.tableView getCellViewInRow:0 inSection:0];
        clienterCell.objectData = clienterDictionary;
        clienterCell.inputFiled = [clienterDictionary objectFormatStringForKey:@"客户名称"];
    }
}

@end
